package com.example.demo.service.impl;

import com.example.demo.entity.TeamAssignment;
import com.example.demo.mapper.TeamAssignmentMapper;
import com.example.demo.service.TeamAssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class TeamAssignmentServiceImpl implements TeamAssignmentService {

    @Autowired
    private TeamAssignmentMapper teamAssignmentMapper;

    @Override
    @Transactional
    public TeamAssignment createTeamAssignment(TeamAssignment teamAssignment) {
        // 设置默认值
        if (teamAssignment.getScore() == null) {
            teamAssignment.setScore(BigDecimal.ZERO);
        }
        if (teamAssignment.getIsExcellent() == null) {
            teamAssignment.setIsExcellent(0);
        }
        if (teamAssignment.getAssignmentStatus() == null) {
            teamAssignment.setAssignmentStatus(TeamAssignment.AssignmentStatus.UNDONE);
        }

        teamAssignmentMapper.insert(teamAssignment);
        return teamAssignment;
    }

    @Override
    public TeamAssignment getTeamAssignmentById(Long id) {
        return teamAssignmentMapper.selectById(id);
    }

    @Override
    public List<TeamAssignment> getTeamAssignmentsByTeamId(Long teamId) {
        return teamAssignmentMapper.selectByTeamId(teamId);
    }

    @Override
    public List<TeamAssignment> getTeamAssignmentsByAssignmentId(String assignmentId) {
        return teamAssignmentMapper.selectByAssignmentId(assignmentId);
    }

    @Override
    public List<TeamAssignment> getTeamAssignmentsBySubmittedBy(Long submittedBy) {
        return teamAssignmentMapper.selectBySubmittedBy(submittedBy);
    }

    @Override
    public List<TeamAssignment> getAllTeamAssignments() {
        return teamAssignmentMapper.selectAll();
    }

    @Override
    @Transactional
    public TeamAssignment updateTeamAssignment(TeamAssignment teamAssignment) {
        int result = teamAssignmentMapper.update(teamAssignment);
        if (result > 0) {
            return teamAssignmentMapper.selectById(teamAssignment.getId());
        }
        return null;
    }

    @Override
    @Transactional
    public boolean deleteTeamAssignment(Long id) {
        return teamAssignmentMapper.deleteById(id) > 0;
    }

    @Override
    public int countTeamAssignmentsByTeamId(Long teamId) {
        return teamAssignmentMapper.countByTeamId(teamId);
    }

    @Override
    public int countTeamAssignmentsByAssignmentId(String assignmentId) {
        return teamAssignmentMapper.countByAssignmentId(assignmentId);
    }

    @Override
    @Transactional
    public boolean updateScoreAndExcellent(Long id, BigDecimal score, Integer isExcellent) {
        return teamAssignmentMapper.updateScoreAndExcellent(id, score, isExcellent) > 0;
    }

    @Override
    @Transactional
    public boolean submitAssignment(Long id, String filePath) {
        return teamAssignmentMapper.updateStatusAndFile(id,
                TeamAssignment.AssignmentStatus.FINISH.getValue(), filePath) > 0;
    }

    @Override
    public List<TeamAssignment> getExcellentTeamAssignments() {
        return teamAssignmentMapper.selectExcellentAssignments();
    }

    @Override
    public TeamAssignment getByTeamIdAndAssignmentId(Long teamId, String assignmentId) {
        return teamAssignmentMapper.selectByTeamIdAndAssignmentId(teamId, assignmentId);
    }
}